<?php
namespace App\Services;

use Ixudra\Curl\Facades\Curl;
use DB;

class Goods{

	public function getCardPrice($sId, $sgId, $muCode, $num=1){

		$sgInfo = DB::table('store_goods')->where('sgId', $sgId)->first();
		$alId = DB::table('member_users')->where('mcId', $sId)->where('muCode', $muCode)->value('alId');
		if (empty($alId)){
			return $sgInfo->sgPrice*$num;
		}
//		print_r($alId);exit;
		$alInfo = DB::table('agency_level')->where('alId', $alId)->first();
		$alInfo = json_decode(json_encode($alInfo), true);
		if ($sgInfo->sgIsDistribution == 0){
			return $sgInfo->sgPrice*$num;
		}
		$alRatio = $alInfo[$sgInfo->alRatio];

		if ($sgInfo->sgIsIncludeService){
			$basePrice = $sgInfo->sgPrice - ($sgInfo->sgTimeCardNum * 20);
			$offsetPrice = $sgInfo->sgTimeCardNum * 20;
		}else{
			$basePrice = $sgInfo->sgPrice;
			$offsetPrice = 0;
		}
		return (($basePrice - ($basePrice * $alRatio / 100)) + $offsetPrice) * $num;
	}

    // 获取代理商对应的商品价格
    public function getTradePrice ($auCode, $sgId){

        $alLevel = DB::select("SELECT agp.goodsPrice
FROM agency_goods_price AS agp
LEFT JOIN agency_level AS al ON agp.acId = al.acId AND agp.alLevel = al.alLevel
LEFT JOIN admin_users AS au ON au.alId = al.alId AND au.sId = agp.acId
WHERE au.auCode = ? AND agp.goodsId = ?", [$auCode, $sgId]);

        return $alLevel[0]->goodsPrice;
    }
}

?>
